/* USER CODE BEGIN Header */
/* you can remove the copyright */

/*
 *  Copyright 2020-2024 Yuntu Microelectronics co.,ltd
 *  All rights reserved.
 * 
 *  YUNTU Confidential. This software is owned or controlled by YUNTU and may only be
 *  used strictly in accordance with the applicable license terms. By expressly
 *  accepting such terms or by downloading, installing, activating and/or otherwise
 *  using the software, you are agreeing that you have read, and that you agree to
 *  comply with and are bound by, such license terms. If you do not agree to be
 *  bound by the applicable license terms, then you may not retain, install,
 *  activate or otherwise use the software. The production use license in
 *  Section 2.3 is expressly granted for this software.
 * 
 * @file main.c
 * @brief 
 * 
 */

/* USER CODE END Header */
#include "sdk_project_config.h"
/* Includes ------------------------------------------------------------------*/

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "common.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint32_t system_reboot_tick;  // ��¼������ʱ��
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void start_init(void)
{
	locator_gpio_init();

  	FLASH_init();
	init_config_param(); // 读取EEPROM初始化参数
    mqtt_subcribe_tpoic_init();  // 必须在读完flash之后。
    mqtt_publish_tpoic_init(); // 必须在读完flash之后。
    clear_mount_point_recond();
	ADC_init();
//
	pTMR_init();
	EC20_init();
	lpuart_debug_init();
	lpuart_bynav_init();

	shell_printf("init ok\n");
	send_fail_handler_init();
	mqtt_subcribe_tpoic_init();
}

/* USER CODE END 0 */


/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
    start_init();
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        ADC_read();
		Bynav_recv_data_handler();
		shell_poll();
		parse_rm500u_response();
		socket_close_ctl();
		connect_5G_service();
		if(socketErrHandler.type == SEND_SUCCESS)
			mqtt_socket_poll();
		if(socketErrHandler.type == SEND_SUCCESS)
			Ntrip_socket_poll();
		else
			send_fail_handler_enter();
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    CLOCK_SYS_Init(g_clockManConfigsArr,CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr,CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(CLOCK_MANAGER_ACTIVE_INDEX,CLOCK_MANAGER_POLICY_AGREEMENT);
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0,g_pin_mux_InitConfigArr0);
}

/* USER CODE BEGIN 4 */
void system_reboot(void)
{
	debug_sendData((const uint8_t *)"system_reboot",14);
	ATcmd_period = ATCMD_INIT;
	init_5g_service_moore = INIT_MOORE_INIT;
	mqtt_socket_moore_state = MOORE_OPEN;
	ntrip_socket_moore_state = MOORE_OPEN;
	socketErrHandler.exit_handler();
	socketErrHandler.socket_timeout_exe = 0;
	clear_ATcmd_response_flag();
	clear_ec20_rx_flag();
	memset(&AT_sem, 0, sizeof(ATcmd_Semaphore_t));
	mqtt_alive.systick = 0;
	memset(&RTK_RecvDiffData, 0, sizeof(RTK_RecvDiffData_t));
	sys_time_set(&system_reboot_tick);
	reset_all_moore();

	power_EC20_ctl(OFF);
	delay_ms(2000);
}
/* USER CODE END 4 */
